// component/calculateQuote/calculateQuote.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    codeCd: 0,
    customItem: '全部',
    floors: [],
    index: 0,
    region: ['广东省', '广州市', '海珠区'],
    region_: '湖南 新化',
    area: '',
    mobile: '',
    validCode: '',
    village: ''
  },

  ready() {
    wx.$https('floor.list').then(res => {
      this.setData({
        floors: res
      })
    })
  },

  methods: {
    villageChange(e) {
      this.setData({
        village: e.detail.value
      })
    },
    offer() {
      let that = this
      let args = {
        mobile: that.data.mobile,
        validCode: that.data.validCode,
        acreage: that.data.area,
        region: that.data.region_,
        village: that.data.village
        // floorId: that.data.floors[that.data.index].floorId
      }
      console.log(args)
      for (let x in args) {
        if (args[x] == '') {
          wx.showToast({
            title: '请输入完整',
            icon: 'none'
          })
          return
        }
      }
      wx.$https('offer.add', {
        ...args
      }).then(res => {
        wx.showToast({
          title: '报价生成成功',
        })
        this.triggerEvent('offerAdd', res)
      })
    },
    getCode() {
      let mobile = this.data.mobile
      if (this.data.codeCd != 0) return
      if (mobile == '') {
        wx.showToast({
          title: '请输入手机号码',
          icon: 'none'
        })
        return
      }
      wx.$https('sms.code.get', {
        mobile: mobile,
        type: 2
      }).then(res => {
        wx.showToast({
          title: '验证码已发送',
          icon: 'none'
        })
        this.setData({
          codeCd: 60
        })
        this.codeSub()
        console.log(res)
      })
    },
    codeSub() {
      let that = this
      let codeCd = that.data.codeCd
      if (codeCd == 0) return
      setTimeout(() => {
        that.setData({
          codeCd: that.data.codeCd - 1
        })
        that.codeSub()
      }, 1000)
    },
    floorChange(e) {
      let floor = e.detail
      console.log(e)
    },
    bindRegionChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        region: e.detail.value
      })
    },
    area(e) {
      this.setData({
        area: e.detail.value
      })
    },
    mobile(e) {
      this.setData({
        mobile: e.detail.value
      })
    },
    isLogin(){
      wx.$isLogin()
    },
    validCode(e) {
      this.setData({
        validCode: e.detail.value
      })
    },
  }
})